<?php
namespace App\Admin\Controllers\OverSeasBa;

use App\Admin\Ajax\OverSeasBa\WarehouseAjax;
use App\Models\Basics\AmazonCountry;
use App\Models\Basics\SysIArea;
use App\Models\Basics\SysPlat;
use App\Models\OverSeasBa\Warehouse;
use App\Models\OverSeasBa\WarehouseGroup;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
class WarehouseController extends AdminController{

    protected $title = '海外仓库信息';

    protected function grid(){

        \Encore\Admin\Admin::style($this->_style());
        $grid = new Grid(new Warehouse());
        $grid->model()->orderBy('id', 'DESC');
        $grid->column('id','ID');
        $grid->column('plat_id','平台')->using(SysPlat::$_platformType);
        $grid->column('warehouse_group.name','海外仓');
        $grid->column('w_code','仓库');
        $grid->column('status','状态')->using(Warehouse::$_status)->filter(Warehouse::$_status)->label([1 => 'info', 0 => 'danger']);
        $grid->column('area.area','站点');
        $grid->column('in_code','入库代码');
        $grid->column('country.country','国家');
        $grid->column('address','详细地址');
        $grid->column('postcode','邮编');
        $grid->column('contact','联系人');
        $grid->column('phone','电话');
        $grid->column('warehouse_models','仓库功能')->pluck('model_id')->display(function ($value){
            $titleStr = '';
            if ($value) {
                foreach ($value as $key => $val){
                    $titleStr = $titleStr . " <span class='label label-success'>". Warehouse::$_warehouse[$val] ."</span> ";
                }
            }
            return $titleStr;
        });

        if(Admin::user()->can('os-ba.warehouse.edit')){
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableView();
                $actions->disableDelete();
            });
        }else{
            $grid->disableActions();
            $grid->disableCreateButton();
        }

        $grid->disableExport();
        $grid->disableRowSelector();
        $grid->paginate(10);
        $grid->fixColumns(0,-1);

        $grid->filter(function($filter){
            $filter->expand();

            $request = request()->all();

            $filter->column(1/3, function ($filter){
                $filter->equal('group_id','海外仓')->select(WarehouseGroup::options());
                $filter->equal('area_id','站点')->select(SysIArea::options());
            });
            $filter->column(1/3, function ($filter) use($request){
                $filter->equal('id','仓库')->select(Warehouse::options());
                $filter->in('warehouse_models.model_id','仓库功能')->checkbox(Warehouse::$_warehouse);
            });
            $filter->column(1/3, function ($filter){
                $filter->equal('plat_id','平台')->select(SysPlat::options());
                $filter->equal('country_id','国家')->select(AmazonCountry::options());
            });
        });
        return $grid;
    }

    public function create(Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->description['create'] ?? trans('admin.create'))
            ->body(view('overseas-ba.warehouse.create.index'));
    }

    public function edit($id, Content $content)
    {
        $w = Warehouse::query()->find($id);
        return $content
            ->title($this->title())
            ->description($this->description['edit'] ?? trans('admin.edit'))
            ->body(view('overseas-ba.warehouse.edit.index',compact('w')));
    }

    public function ajax(){
        $action = request('action');
        $param = request('param');
        $ajax = new WarehouseAjax();
        return $param ? call_user_func([$ajax,$action],$param) : call_user_func([$ajax,$action]);
    }

    protected function _style(){

        return <<<STYLE
            .table > thead > tr > th, .table > tbody > tr > td {
                white-space: nowrap;
            }
            .table-responsive {
                max-height: calc(80vh);
            }
STYLE;
    }

}

